//
//  ReadViewController.m
//  Leisure
//
//  Created by xalo on 16/6/28.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "ReadViewController.h"

@interface ReadViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *scrollArray;//轮播图图片
@property (nonatomic,strong)NSMutableArray *dataSource;//数据源
@end

@implementation ReadViewController

- (NSArray *)scrollArray{
    if (!_scrollArray) {
        _scrollArray=[NSMutableArray array];
    }
    return _scrollArray;
}

- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource=[NSMutableArray array];
    }
    return _dataSource;
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
        layout.itemSize=CGSizeMake(100, 100);
        layout.sectionInset=UIEdgeInsetsMake(20, 10, 20, 10);
        _collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, kHeight/3 + 64, kWidth, kHeight/2) collectionViewLayout:layout];
        _collectionView.dataSource=self;
        _collectionView.delegate=self;
        _collectionView.backgroundColor=[UIColor whiteColor];
    }
    return _collectionView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets=NO;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ReadCell" bundle:nil] forCellWithReuseIdentifier:@"read_cell"];
    
    [RequestManager fetchDataWithUrl:kReadUrl parameter:@{} method:@"GET" complete:^(NSData *data) {
        NSDictionary *bigDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
       // NSLog(@"%@",bigDic);
        for (NSDictionary *dic in bigDic[@"data"][@"carousel"]) {
            
            [self.scrollArray addObject:dic[@"img"]];
            
        }
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            ReadModel *model=[[ReadModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.dataSource addObject:model];
        }
    } updateUI:^{
        //创建scrollView
        WHZScrollView *scrollView=[[WHZScrollView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight/3) imageArray:self.scrollArray];
        [self.view addSubview:scrollView];
        
       // NSLog(@"刷新UI");
        [self.collectionView reloadData];
    }];
    
    
    
    
    
  
    
    
    
}











- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ReadCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"read_cell" forIndexPath:indexPath];
    [cell setCellWithModel:self.dataSource[indexPath .row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ReadListViewController *readListVC=[[ReadListViewController alloc]initWithNibName:@"ReadListViewController" bundle:nil];
    ReadModel *model=self.dataSource[indexPath.row];
    readListVC.typeID=model.type;
    
    [self.navigationController pushViewController:readListVC animated:YES];
}


















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
